/************************************************************************
  * Copyright(c) 2023 Levetop Semiconductor Co.,Led. All rights reserved.
  * @file     tp_ctp.h
  * @author   UartTFT Application Team
  * @version  V0.0.1
  * @date     2023-01-01
  * @brief    
 *************************************************************************/

#ifndef _TP_CTP_H
#define _TP_CTP_H

#include "delay.h"
#include "data.h"
#include "bsp.h"
#include "i2c_drv.h"

/*--------------------------------------------CTP GPIO ---------------------------------*/
#define CTP_SCL_OUTPUT		I2C_ConfigGpio(I2C1, I2C_SCL, GPIO_OUTPUT)
#define CTP_SCL_PULLUP		I2C->PCR |= (Bit_SET << I2C_SCL)
#define CTP_SCL_HIGH		I2C_WriteGpioData(I2C1, I2C_SCL, Bit_SET)
#define CTP_SCL_LOW			I2C_WriteGpioData(I2C1, I2C_SCL, Bit_RESET)

#define CTP_SDA_OUTPUT 		I2C_ConfigGpio(I2C1, I2C_SDA, GPIO_OUTPUT)
#define CTP_SDA_INPUT 		I2C_ConfigGpio(I2C1, I2C_SDA, GPIO_INPUT)
#define CTP_SDA_PULLUP		I2C->PCR |= (Bit_SET << I2C_SDA)
#define CTP_GetSDABit 		I2C_ReadGpioData(I2C1, I2C_SDA)
#define CTP_SDA_HIGH 		I2C_WriteGpioData(I2C1, I2C_SDA, Bit_SET)
#define CTP_SDA_LOW 		I2C_WriteGpioData(I2C1, I2C_SDA, Bit_RESET)

#define CTP_RST_OUTPUT		EPORT_ConfigGpio(EPORT_PIN34, GPIO_OUTPUT)
#define CTP_RST_HIGH		EPORT_WriteGpioData(EPORT_PIN34, Bit_SET)
#define CTP_RST_LOW 		EPORT_WriteGpioData(EPORT_PIN34, Bit_RESET)

#define CTP_INT_OUTPUT		EPORT_ConfigGpio(EPORT_PIN33, GPIO_OUTPUT)
#define CTP_INT_INPUT		EPORT_ConfigGpio(EPORT_PIN33, GPIO_INPUT)
#define CTP_INT_INTERRUPT	EPORT_Init(EPORT_PIN33, RISING_EDGE_INT)
#define CTP_GET_INT_BIT		EPORT_ReadGpioData(EPORT_PIN33)
#define CTP_INT_HIGH		EPORT_WriteGpioData(EPORT_PIN33, Bit_SET)
#define CTP_INT_LOW 	 	EPORT_WriteGpioData(EPORT_PIN33, Bit_RESET)

#define CT_COM_OK (0)
#define CT_ACK_FAIL (1)
#define CT_WRITE_MASK (0x00)
#define CT_READ_MASK (0x01)
#define CT_CACK_TIMEOUT (3000) // Wait for ACK timeout

extern uint8_t CTP_ADDR;
extern uint8_t READ_BUFF[];
extern uint8_t ctp_active_index;
extern uint8_t ctp_press_t;

extern uint8_t CTP_ConfigureBuff[];
void Checksum(uint8_t *p); 
void ClearBuff(uint8_t *p);
uint8_t TP_Get_INT(void);
uint8_t CTP_BufferWrite(uint8_t *pBuffer, uint32_t WriteAddr, uint8_t AddrByte, uint16_t NumByte);
uint8_t CTP_BufferRead(uint8_t *pBuffer, uint32_t ReadAddr, uint8_t AddrByte, uint16_t NumByte);
uint8_t TP_read_XY(void);
uint8_t CTP_Scan(void);
void CTP_IO_Init(void);
void CTP_Init(void);

#if IIC_BUS
void I2C_Slave_TEST(uint8_t sla_addr);

extern uint8_t flag_IIC_REG[];
extern uint16_t ID_IIC_WAV;
#endif

#endif /* _TP_CTP_H */
